package com.xrh.core.generater.mybatisplus;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import com.xrh.core.generater.bean.ColumnBean;
import com.xrh.core.generater.bean.TableInfo;
import com.xrh.core.generater.bean.UserInput;
import com.xrh.core.generater.interfaces.TemplateRootPath;
import com.xrh.core.generater.utils.FileUtils;
import com.xrh.core.generater.utils.MarkEnum;
import com.xrh.core.generater.utils.TemplateNameEnum;
import com.xrh.core.generater.utils.TemplateReader;

//domain文件生成器
public class EntityGenerater {

		//生成文件
		public static void generateFile(UserInput userInput , TableInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt = generateTemplateTxt(userInput, tableInfo, templateRootPath);
			
			FileUtils.generateFile(userInput.getOutputDirPath() + File.separator + "entity" , tableInfo.getClassName() + "Entity.java", templateTxt);
	
		}
		
		/**
		* @function 生成模板字符串
		* @author 肖荣辉
		* @date 2018年10月23日
		*/
		public static String generateTemplateTxt(UserInput userInput , TableInfo tableInfo , TemplateRootPath templateRootPath) throws IOException{
			
			String templateTxt =  TemplateReader.readTextTemplate(templateRootPath , TemplateNameEnum.Entity模板.name());
			
			//通用替换
			templateTxt = MarkEnum.replaceMark(templateTxt , userInput , tableInfo);
			
			//私有属性文本
			templateTxt = MarkEnum.replace(MarkEnum.PRIVATE_PROPERTY_MARK , templateTxt , generatePrivatePropertyTxt(tableInfo));
			
			return templateTxt;
		}
		

		//生成私有属性文本
		private static String generatePrivatePropertyTxt(TableInfo tableInfo) {
			
			Set<Map.Entry<String, ColumnBean>> set = tableInfo.getPropertyInfoMap().entrySet();
			
			StringBuilder sb = new StringBuilder();
			
			 String tab1 = "\n\t";
			 
			 String txt = 	tab1 + "/**"+MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name()+" */"+
		 				MarkEnum.API_MODEL_PROPERTY_MARK.name() +
		 				tab1 + "private "+MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name()+ " "+MarkEnum.PROPERTY_NAME_MARK.name()+";";
			  
			for(Map.Entry<String, ColumnBean> item : set){
				
				  String key = item.getKey();
				  ColumnBean bean = item.getValue();
				  
				  sb.append(txt.replaceAll(MarkEnum.PRIVATE_PROPERTY_COMMENT_MARK.name(), bean.getComment())
				  			.replaceAll(MarkEnum.PROPERTY_JAVA_TYPE_NAME_MARK.name(), bean.getJavaTypeName())
				  			.replaceAll(MarkEnum.API_MODEL_PROPERTY_MARK.name(), generateAnnotationTxt(tab1, bean))
				  			.replaceAll(MarkEnum.PROPERTY_NAME_MARK.name(), key)).append("\n");
				  
			}
			
			return sb.toString().trim();
			
		}
		
		/**
		 * @function 生成注解标记
		 * @author 肖荣辉
		 * @date 2021年6月29日
		*/
		private static String generateAnnotationTxt(String tab , ColumnBean columnBean) {
			
			String txt = "";
			
			String columnName = columnBean.getColumnName();
			
			/*特殊字段*/
			if("id".equals(columnName)) {
				
				txt +=   tab +  "@JsonFormat(shape = JsonFormat.Shape.STRING)" + 
						 	 tab +  "@TableId(value=\"id\" ,type = IdType.ASSIGN_ID)";
				
			}else if("create_user_id".equals(columnName)) {
				
				txt+= tab + "@TableField(value = \"create_user_id\" , fill = FieldFill.INSERT)";
				
			}else if("create_user_name".equals(columnName)) {
				
				txt+= tab + "@TableField(value = \"create_user_name\" , fill = FieldFill.INSERT)";
				
			}else if("create_user_account".equals(columnName)) {
				
				txt+= tab + "@TableField(value = \"create_user_account\" , fill = FieldFill.INSERT)";
				
			}else if("create_time".equals(columnName)) {
				
				txt+= tab + "@TableField(value=\"create_time\",fill = FieldFill.INSERT)";
				
			}else if("update_user_id".equals(columnName)) {
				
				txt+= tab + "@TableField(value=\"update_user_id\",fill = FieldFill.INSERT_UPDATE)";
				
			}else if("update_user_name".equals(columnName)) {
				
				txt+= tab + "@TableField(value=\"update_user_name\",fill = FieldFill.INSERT_UPDATE)";
				
			}else if("update_user_account".equals(columnName)) {
				
				txt+= tab + "@TableField(value=\"update_user_account\",fill = FieldFill.INSERT_UPDATE)";
				
			}else if("update_time".equals(columnName)) {
				
				txt+= tab + "@TableField(value = \"update_time\",fill = FieldFill.INSERT_UPDATE)";
				
			}else if("del_flag".equals(columnName)) {
				txt += tab + "@TableLogic(value=\"0\" , delval=\"REPLACE(unix_timestamp(current_timestamp(3)),'.','')\")" + 
						   tab + "@TableField(value=\"del_flag\")";
			}else if("update_version".equals(columnName)) {
				
				txt += tab + "@Version" + 
						   tab + "@TableField(value=\"update_version\",fill = FieldFill.INSERT)";
	
			}else {
				txt+= tab +  "@TableField(\""+columnName+"\")";
			}
			
			if(columnBean.isDate()) {
				
				txt += tab + "@DateTimeFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")" + 
						   tab + "@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\", timezone = \"GMT+8\")";
				
			}
			
			return txt;
		}
		
}
